package com.ruoyi.womp.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.womp.domain.WompOsiRecord;
import com.ruoyi.womp.service.ISqlQueryService;
import com.ruoyi.womp.service.IWompOsiRecordService;

/**
 * 巡检记录Controller
 * 
 * @author mzz
 * @date 2023-05-11
 */
@Controller
@RequestMapping("/womp/osiRecord")
public class WompOsiRecordController extends BaseController {
	private String prefix = "womp/osiRecord";

	@Autowired
	private IWompOsiRecordService wompOsiRecordService;

	@Autowired
	ISqlQueryService sqlQueryService;

	@RequiresPermissions("womp:osiRecord:view")
	@GetMapping()
	public String osiRecord() {
		return prefix + "/osiRecord";
	}

	/**
	 * 查询巡检记录列表
	 */
	@RequiresPermissions("womp:osiRecord:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WompOsiRecord wompOsiRecord) {
		startPage();
		List<WompOsiRecord> list = wompOsiRecordService.selectWompOsiRecordList(wompOsiRecord);
		return getDataTable(list);
	}

	/**
	 * 导出巡检记录列表
	 */
	@RequiresPermissions("womp:osiRecord:export")
	@Log(title = "巡检记录", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(WompOsiRecord wompOsiRecord) {
		List<WompOsiRecord> list = wompOsiRecordService.selectWompOsiRecordList(wompOsiRecord);
		ExcelUtil<WompOsiRecord> util = new ExcelUtil<WompOsiRecord>(WompOsiRecord.class);
		return util.exportExcel(list, "巡检记录数据");
	}

	/**
	 * 新增巡检记录
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存巡检记录
	 */
	@RequiresPermissions("womp:osiRecord:add")
	@Log(title = "巡检记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WompOsiRecord wompOsiRecord) {
		return toAjax(wompOsiRecordService.insertWompOsiRecord(wompOsiRecord));
	}

	/**
	 * 修改巡检记录
	 */
	@RequiresPermissions("womp:osiRecord:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		WompOsiRecord wompOsiRecord = wompOsiRecordService.selectWompOsiRecordById(id);
		mmap.put("wompOsiRecord", wompOsiRecord);
		return prefix + "/edit";
	}

	/**
	 * 修改保存巡检记录
	 */
	@RequiresPermissions("womp:osiRecord:edit")
	@Log(title = "巡检记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WompOsiRecord wompOsiRecord) {
		return toAjax(wompOsiRecordService.updateWompOsiRecord(wompOsiRecord));
	}

	/**
	 * 删除巡检记录
	 */
	@RequiresPermissions("womp:osiRecord:remove")
	@Log(title = "巡检记录", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(wompOsiRecordService.deleteWompOsiRecordByIds(ids));
	}

	/**
	 * 巡检登记
	 */
	@RequiresPermissions("womp:osiPlan:reg")
	@GetMapping("/reg/{id}")
	public String reg(@PathVariable("id") Integer id, ModelMap mmap, HttpServletRequest request) {
		String sql = " select id,name,remark from womp_osi_item woi "
				+ " where FIND_IN_SET(woi.id,(select osi_items from womp_osi_plan wop where wop.id=" + id + ")) ";
		List<Map<String, Object>> list = sqlQueryService.queryMapList(sql);
		mmap.put("dataList", list);
		mmap.put("waCode", request.getParameter("waCode"));
		mmap.put("osiPlanType", request.getParameter("planType"));
		String loginName = (String) PermissionUtils.getPrincipalProperty("loginName");
		mmap.put("loginName", loginName);
		return prefix + "/reg";
	}

	/**
	 * 保存巡检登记
	 */
	@RequiresPermissions("womp:osiPlan:reg")
	@Log(title = "巡检计划", businessType = BusinessType.UPDATE)
	@PostMapping("/reg")
	@ResponseBody
	public AjaxResult reg(WompOsiRecord wompOsiRecord) {
		String loginName = (String) PermissionUtils.getPrincipalProperty("loginName");
		wompOsiRecord.setCreateBy(loginName);
		return toAjax(wompOsiRecordService.insertWompOsiRecord(wompOsiRecord));
	}
}
